<?php

namespace App\Http\Controllers\Xxjh;

use App\Http\Controllers\ApiController;
use App\Models\XxjhBaoming;
use Illuminate\Support\Facades\Input;


class WebController extends ApiController
{
    public function index()
    {

        $source = XxjhBaoming::$source;


        return view('xxjh.web.index', compact('source'));
    }


    public function postSignup()
    {
        $data = Input::all();

        $rules = [
            'type' => 'required',
            'relname' => 'required',
            'song' => 'required',
            'email' => 'required|email',
            'mobile' => 'required',
            'source' => 'required',
            'age' => 'integer|max:99',
            'age2' => 'integer|max:99',
//            'clause' => 'required',
        ];

        if ($data['type'] == 2) {
            $rules2 = [
                'relname2' => 'required',
            ];
            $rules = array_merge($rules, $rules2);
        }

        $messages = [
            'type.required' => '参赛方式不能为空',
            'relname.required' => '参赛者姓名不能为空',
            'song.required' => '参赛曲目不能为空',
            'email.required' => '邮箱不能为空',
            'mobile.required' => '联系方式不能为空',
            'source.required' => '您从何渠道知晓本次活动',
            'age.integer' => '年龄必须是整数',
            'age2.integer' => '年龄必须是整数',
            'clause.required' => '隐私条款必读',
        ];

        parent::validator($data, $rules, $messages);

        $model = new XxjhBaoming();
        $model->type = $data['type'];
        $model->relname = $data['relname'];
        $model->age = isset($data['age']) ? $data['age'] : 0;
        $model->relname2 = isset($data['relname2']) ? $data['relname2'] : '';
        $model->age2 = isset($data['age2']) ? $data['age2'] : 0;
        $model->song = isset($data['song']) ? $data['song'] : '';
        $model->video = isset($data['video']) ? $data['video'] : '';
        $model->email = isset($data['email']) ? $data['email'] : '';
        $model->mobile = isset($data['mobile']) ? $data['mobile'] : '';

        $model->source = isset($data['source']) ? $data['source'] : '';
        if (!empty($data['source_other'])) {
            $model->source = isset($data['source_other']) ? $data['source_other'] : '';
        }
        $rt = $model->save();

        if ($rt) {
            parent::success(['success' => $rt]);
        } else {
            parent::error('系统忙，稍后再试');
        }

    }
}
